package ywpt.web.action.people;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.util.DateTools;
import ywpt.core.util.StringTools;
import ywpt.core.web.BaseAction;

import ywpt.model.BsnssOptntInfo;
import ywpt.service.people.BsnssOptntInfoService;

import ywpt.web.common.CommonDictionary;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class BsnssOptntAction extends BaseAction implements ModelDriven<BsnssOptntInfo>, Preparable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2639001785055084866L;
	/**
	 * 商机管理Action
	 */

	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];//
	private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	private static int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	private BsnssOptntInfo bsnssOptntInfo;
	private List<BsnssOptntInfo> bsnssOptntInfoList = new ArrayList<BsnssOptntInfo>();
	private BsnssOptntInfoService bsnssOptntInfoService;

	private String qStartDate;
	private String qEndDate;
 	
	private int version;	
	private Long clientId;
	private String clientName;
	private Long stateId;
	private String StateName;
	
	public Long getClientId() {
		return clientId;
	}
	public void setClientId(Long clientId) {
		this.clientId = clientId;
	}
	public Long getStateId() {
		return stateId;
	}
	public void setStateId(Long stateId) {
		this.stateId = stateId;
	}
	
	
	public String getClientName() {
		return clientName;
	}
	public void setClientName(String clientName) {
		this.clientName = clientName;
	}




	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}
//*****************************************************************************
	@Override
	public BsnssOptntInfo getModel() {
		return this.bsnssOptntInfo;
	}

	@Override
	public void prepare() throws Exception {
		if(id != null){
			this.bsnssOptntInfo = this.bsnssOptntInfoService.getBsnssOptntInfoById(id);
		}else{
			this.bsnssOptntInfo = new BsnssOptntInfo();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.PEOPLE_BSNSSOPTNT);
	}
	
	protected void showInfo() {
		if (this.bsnssOptntInfo.getClientInfo()!=null) {
			this.clientId=this.bsnssOptntInfo.getClientInfo().getId();
			this.clientName=this.bsnssOptntInfo.getClientInfo().getClientName();
		}
		if(this.bsnssOptntInfo.getChance_state()!=null){
			this.setStateId(this.bsnssOptntInfo.getChance_state().getId());
		}
	}
	//*****************************************************************************
	@SuppressWarnings("unchecked")
	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(BsnssOptntInfo.class);
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = bsnssOptntInfoService.getBsnssOptntInfoCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.desc("create_date"));
		query.setProjection(null);
		bsnssOptntInfoList.addAll(bsnssOptntInfoService.getBsnssOptntInfos(query, pageSize, rowStartEnd[0]));
		getRequest().setAttribute("PAYMETHOD_MAP", CommonDictionary.PAYMETHOD);
		return SUCCESS;
	}

	public String input() 
	{	
		bsnssOptntInfo.setCreate_date(new Date());

		return INPUT;
	}
	
	public String edit() 
	{
		this.bsnssOptntInfo=this.bsnssOptntInfoService.getBsnssOptntInfoById(id);
		showInfo();
		return INPUT;
	}
	

	public String save() throws Exception 
	{	try 
		{	
			if (id == null) { //新增
			
		
				bsnssOptntInfo.setCreate_user(this.getCurrentUser());
				bsnssOptntInfo.setCreate_date(new Date());
				bsnssOptntInfoService.saveBsnssOptntInfo(bsnssOptntInfo,clientId,stateId);
	
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
	
			} else {
				if(this.getVersion()==bsnssOptntInfo.getVersion())
				{
					bsnssOptntInfo.setModify_user(this.getCurrentUser());
					bsnssOptntInfo.setModify_date(new Date());
					bsnssOptntInfoService.updateBsnssOptntInfo(bsnssOptntInfo,clientId,stateId);;
					this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
				}else
					this.addActionMessage(SystemMessage.UPDATE_FAIL);
			}
			showInfo();
			return SUCCESS;		
		} 
		catch (UniqueConstraintException e) 
		{
			this.addActionError(SystemMessage.SAVE_FAIL + e.getMessage());
			return INPUT;
		}
	}
	
	/**
	 * ɾ��
	 * 
	 * @return
	 */
	public String delete() throws Exception 
	{		
		if (id != null) 
		{	
			bsnssOptntInfoService.removeBsnssOptntInfo(id);
			return SUCCESS;				
		}else {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
	}

	public String search() throws Exception 
	{
		DetachedCriteria query = DetachedCriteria.forClass(BsnssOptntInfo.class);


		if(!StringTools.isNullOrSpace(this.qStartDate)&& !StringTools.isNullOrSpace(this.qEndDate))
		{				
			if(FORMAT.parse(this.qStartDate).before(FORMAT.parse(this.qEndDate)))
				query.add(Restrictions.between("create_date",FORMAT.parse(this.qStartDate),DateTools.addDay(FORMAT.parse(this.qEndDate), 0)));
			else  
				query.add(Restrictions.between("create_date",FORMAT.parse(this.qEndDate),DateTools.addDay(FORMAT.parse(this.qStartDate),0)));
		}
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) 
		{
			totalRows =bsnssOptntInfoService.getBsnssOptntInfoCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.desc("code"));
		query.setProjection(null);
		bsnssOptntInfoList.addAll(bsnssOptntInfoService.getBsnssOptntInfos(query, pageSize, rowStartEnd[0]));
		return SUCCESS;
	}
	
	public String select() 
	{
		bsnssOptntInfoList.addAll(bsnssOptntInfoService.getBsnssOptntInfos());
		return SUCCESS;
	}

	public String getQStartDate() {
		return qStartDate;
	}

	public void setQStartDate(String startDate) {
		qStartDate = startDate;
	}

	public String getQEndDate() {
		return qEndDate;
	}

	public void setQEndDate(String endDate) {
		qEndDate = endDate;
	}
	/**
	 * @return bsnssOptntInfo
	 */
	public BsnssOptntInfo getBsnssOptntInfo() {
		return bsnssOptntInfo;
	}
	/**
	 * @param bsnssOptntInfo 要设置的 bsnssOptntInfo
	 */
	public void setBsnssOptntInfo(BsnssOptntInfo bsnssOptntInfo) {
		this.bsnssOptntInfo = bsnssOptntInfo;
	}
	/**
	 * @return bsnssOptntInfoList
	 */
	public List<BsnssOptntInfo> getBsnssOptntInfoList() {
		return bsnssOptntInfoList;
	}
	/**
	 * @param bsnssOptntInfoList 要设置的 bsnssOptntInfoList
	 */
	public void setBsnssOptntInfoList(List<BsnssOptntInfo> bsnssOptntInfoList) {
		this.bsnssOptntInfoList = bsnssOptntInfoList;
	}
	/**
	 * @return bsnssOptntInfoService
	 */
	public BsnssOptntInfoService getBsnssOptntInfoService() {
		return bsnssOptntInfoService;
	}
	/**
	 * @param bsnssOptntInfoService 要设置的 bsnssOptntInfoService
	 */
	public void setBsnssOptntInfoService(BsnssOptntInfoService bsnssOptntInfoService) {
		this.bsnssOptntInfoService = bsnssOptntInfoService;
	}
	/**
	 * @return version
	 */
	public int getVersion() {
		return version;
	}
	/**
	 * @param version 要设置的 version
	 */
	public void setVersion(int version) {
		this.version = version;
	}





	
}
